<?php
namespace app\datamanage\controller;
use think\Controller;

/**
 *
 */
class Zhenpin extends Logincheck
{

    function showZhenpin($teacase='', $teaarea='')
    {
        if($teacase==''&&$teaarea==''){
            //返回茶类
            $retval = getTeaCase();
            $this->assign('teacase', $retval);
            $this->assign('data', '');
            $this->assign("currentteacase", $teacase);
            $this->assign("currentteaarea", $teaarea);
            $this->assign('areas','');
            return $this->fetch();
        }

        //返回茶类
        $retval = getTeaCase();
        $this->assign('teacase', $retval);
        //根据传参选择返回数据
        if($teacase==10000){
            $retval = model("Zhenpin")->showZhenpin();
        }else if($teacase==9999){
            $condition = ['bangdan_id'=>4];
            $retval = model("Zhenpin")->showZhenpin($condition);
        }else{
            if($teaarea==9999){
                $condition = ['teacase_id'=>$teacase, 'bangdan_id'=>4];
                $retval = model("Zhenpin")->showZhenpin($condition);
            }else if($teaarea==10000){
                $condition = ['teacase_id'=>$teacase];
                $retval = model("Zhenpin")->showZhenpin($condition);
            }else{
                $condition = ['teacase_id'=>$teacase, 'teaarea_id'=>$teaarea];
                $retval = model("Zhenpin")->showZhenpin($condition);
            }
        }

        $tid_name = teacase_ids();
        $aid_name = teaarea_ids();
        $rank = 1;
        foreach ($retval as &$oneres) {
            $oneres['rank'] = $rank;
            $rank++;
            $oneres['teacase'] = $tid_name[$oneres['teacase_id']];
            $oneres['teaarea'] = $aid_name[$oneres['teaarea_id']];
            $oneres['new_score'] = $this->evaluate($oneres['detailintro']);
        }

        $this->assign("currentteacase", $teacase);
        $this->assign("currentteaarea", $teaarea);
        $this->assign('data',$retval);

        $retval = $this->reTeaArea($teacase);
        $this->assign('areas',$retval);
        return $this->fetch();
    }

    function updateAll()
    {
        $res = model('Zhenpin')->showZhenpin();
        foreach ($res as &$oneres) {
            $new_score = $this->evaluate($oneres['detailintro']);
            $id = $oneres['id'];
            $data = ['score'=>$new_score];
            //print_pre($data);
            model('Zhenpin')->updateNew($id, $data);
        }
        return $this->redirect('showZhenpin');
    }

    function modify($id, $score)
    {
        $data = ['score'=>$score];
        model('Zhenpin')->updateNew($id, $data);
        if($score<80){
            return 'B';
        }else if($score<85){
            return 'A-';
        }else if($score<90){
            return 'A';
        }else if($score<95){
            return 'A+';
        }else{
            return 'A++';
        }
    }

    //返回某茶类对应的产地JSON数据
    function reTeaAreaJson($teacase_id)
    {
        // $data = $this->reTeaArea($teacase_id);
        $data = $this->reTeaArea($teacase_id);
        return json_encode($data);
    }


    //返回某茶类对应的产地
    function reTeaArea($teacase_id)
    {
        $retval = model("Zhenpin")->reTeaArea($teacase_id);
        $areas = getTeaArea();
        $data = [];
        foreach ($retval as $ret) {
            foreach ($areas as $area) {
                if($ret["teaarea_id"]==$area["id"]){
                    $data[] = $area;
               }
           }
       }
       return $data;
    }

/*****************************************************************************/
    function evaluate($detailintro)
    {
        $pattern = '/不可多得/';
        if(preg_match($pattern, $detailintro))
        {
            $score = 83;
            return $score ;
        }
        $pattern = '/难得一见/';
        if(preg_match($pattern, $detailintro))
        {
            $score = 88;
            return $score ;
        }
        $pattern = '/百里挑一/';
        if(preg_match($pattern, $detailintro))
        {
            $score = 93;
            return $score ;
        }
        $pattern = '/凤毛麟角/';
        if(preg_match($pattern, $detailintro))
        {
            $score = 97;
            return $score ;
        }
        $pattern = '/稀世之宝/';
        if(preg_match($pattern, $detailintro))
        {
            $score = 99;
            return $score ;
        }

        return 79;
    }



















}

?>